package com.bw.home.repo

import com.bw.common.entity.CommendEntity
import com.bw.common.entity.SimpleVideoEntity
import com.bw.home.api.HomeApiServer
import com.bw.home.entity.SimpleTypeEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okhttp3.RequestBody
import javax.inject.Inject

class HomeRepository @Inject constructor(private val apiServer: HomeApiServer){

        suspend fun getSimpleType(): Flow<List<SimpleTypeEntity>> {
            return flow {
                val result = apiServer.getSimpleType()
                if (result.code == 200) {
                    emit(result.data)
                } else {
                    emit(listOf())
                }
            }
        }

        suspend fun getSimpleVideoList(channelId: String, pageIndex: Int, pageSize: Int): Flow<List<SimpleVideoEntity>> {
            return flow {
                var result =
                    apiServer.getSimpleVideoByChannelId(channelId, pageIndex, pageSize)
                if (result.code == 200) {
                    emit(result.data)
                } else {
                    emit(emptyList())
                }
            }
        }

        suspend fun getVideoCommends(itemId: String, dataType: Int): Flow<List<CommendEntity>> {
            return flow {
                var result = apiServer.getVideoCommends(dataType, itemId)
                if (result.code == 200) {
                    emit(result.data)
                } else {
                    emit(emptyList())
                }
            }
        }

        suspend fun publishVideoComment(requestBody: RequestBody): Flow<List<CommendEntity>> {
            return flow {
                var result = apiServer.publishVideoComment(requestBody)
                if (result.code == 200) {
                    emit(result.data)
                } else {
                    emit(emptyList())
                }
            }
        }

    suspend fun getRecommendSimpleVideoList(pageIndex: Int, pageSize: Int): Flow<List<SimpleVideoEntity>> {
        return flow {
            var result = apiServer.getRecommendSimpleVideo(pageIndex, pageSize)
            if (result.code == 200) {
                emit(result.data)
            } else {
                emit(emptyList())
            }
        }
    }




}